package com.boot.service.impl;

import com.boot.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import com.boot.mapper.BasicDataMapper;
import com.boot.mapper.OrganizationMapper;
import com.boot.service.OrgService;

import java.util.*;

/**
 * @author wwj
 */
@Service
public class OrgServiceImpl implements OrgService{
	@Autowired  
	private OrganizationMapper orgDaoMapper;

	@Autowired
	private BasicDataMapper basicDataMapper;

	@Autowired
	private Environment env;
	
	@Override
	public Organization selectOrgById(Integer id) {
		return orgDaoMapper.selectOrgById(id);
	}

	@Override
	public List<Entity> getBasicData(String[] basicDataName) {
		return basicDataMapper.getBasicData(basicDataName);
	}

	@Override
	public List<InspectionByType> getInspectionByType(int orgType){
		List<InspectionByType> list =  orgDaoMapper.getInspectionByType(orgType);
		for(InspectionByType inspectionByType:list){
			if(inspectionByType.getMedia()!=null && !"".equals(inspectionByType.getMedia())) {
				inspectionByType.setMedia(env.getProperty("profile.media_url") + inspectionByType.getMedia().substring(1));
			}
		}
		return list;
	}

	@Override
	public List<LaboratoryByType> getLaboratoryByType(int orgType) {
		List<LaboratoryByType> list = orgDaoMapper.getLaboratoryByType(orgType);
		for(LaboratoryByType laboratoryByType:list){
			if(laboratoryByType.getMedia()!=null && !"".equals(laboratoryByType.getMedia())) {
				laboratoryByType.setMedia(env.getProperty("profile.media_url") + laboratoryByType.getMedia().substring(1));
			}
		}
		return list;
	}

	@Override
	public List<ScientificByType> getScientificByType(int orgType) {
		List<ScientificByType>  list =  orgDaoMapper.getScientificByType(orgType);
		for(ScientificByType scientificByType:list){
			if(scientificByType.getMedia()!=null && !"".equals(scientificByType.getMedia())) {
				scientificByType.setMedia(env.getProperty("profile.media_url") + scientificByType.getMedia().substring(1));
			}
		}

		return list;
	}


	@Override
	public List<OrganizationByDistribution> getOrganizationByDistribution() {
		List<OrganizationByDistribution> organizationByDistributionList = orgDaoMapper.getOrganizationByDistribution();
	    return organizationByDistributionList;
	}

	@Override
	public List<RankingByDistribution> getNewCancelSumByDistribution() {
		return orgDaoMapper.getNewCancelSumByDistribution();
	}

	@Override
	public List<ProportionByDistribution> getNewCancelSumProportion() {
		List<ProportionByDistribution> map = orgDaoMapper.getNewCancelSumProportion();
		return map;
	}

	@Override
	public List<OrganiseByDistribution> getOrgOrganiseSum(String county) {
		return orgDaoMapper.getOrgOrganiseSum(county);
	}

	@Override
	public List<OrganiseByDistribution> getOrgChildOrganiseSum(String county, String parentId) {
		return orgDaoMapper.getOrgChildOrganiseSum(county,parentId);
	}

	@Override
	public List<OrganiseByDistribution> getNewOrgOrganiseSum(String county) {
		return orgDaoMapper.getNewOrgOrganiseSum(county);
	}

	@Override
	public List<OrganiseByDistribution> getNewOrgChildOrganiseSum(String county, String parentId) {
		return orgDaoMapper.getNewOrgChildOrganiseSum(county,parentId);
	}

	@Override
	public List<OrganiseByDistribution> getCancelOrgOrganiseSum(String county) {
		return orgDaoMapper.getCancelOrgOrganiseSum(county);
	}

	@Override
	public List<OrganiseByDistribution> getCancelOrgChildOrganiseSum(String county, String parentId) {
		return orgDaoMapper.getCancelOrgChildOrganiseSum(county,parentId);
	}

	@Override
	public List<OrganiseByDistribution> getTestOrgOrganiseSum(String county) {
		return orgDaoMapper.getTestOrgOrganiseSum(county);
	}

	@Override
	public List<OrganiseByDistribution> getTestOrgChildOrganiseSum(String county, String parentId) {
		return orgDaoMapper.getTestOrgChildOrganiseSum(county,parentId);
	}

	@Override
	public List<Organization> getOrgListByZoneCode(String county) {
		return orgDaoMapper.getOrgListByZoneCode(county);
	}

	@Override
	public List<TurnoverByIndeustry> getOrgTurnoverByIndeustry(String county) {
		List<TurnoverByIndeustry> list = orgDaoMapper.getOrgTurnoverByIndeustry(county);
		for(TurnoverByIndeustry turnoverByIndeustry:list){
			if(turnoverByIndeustry.getMedia()!=null && !"".equals(turnoverByIndeustry.getMedia())) {
				turnoverByIndeustry.setMedia(env.getProperty("profile.media_url") + turnoverByIndeustry.getMedia().substring(1));
			}
		}
		return list;
	}

	@Override
	public List<OrgTradeSum> getOrgTradeSumByZoneCode(String county) {
		return orgDaoMapper.getOrgTradeSumByZoneCode(county);
	}

	@Override
	public List<OrgGDPSum> getOrgGDPSumByZoneCode(String county) {
		return orgDaoMapper.getOrgGDPSumByZoneCode(county);
	}

	@Override
	public Map getNewOrgOrganiseByZoneCode(String conuty, String organiseCode) {
		List<NewOrgOrganise> list = orgDaoMapper.getNewOrgOrganiseByZoneCode(conuty,organiseCode);
		Set<String> set = new HashSet<String>();
		Set<String> organisSet = new HashSet<String>();
		for(NewOrgOrganise newOrgOrganise:list){
			set.add(newOrgOrganise.getCreateTime());
		}
		for(NewOrgOrganise newOrgOrganise:list){
			organisSet.add(newOrgOrganise.getOrganiseName());
		}


		Map timeMap = new HashMap();
		for(NewOrgOrganise newOrgOrganise:list) {
			Iterator iterator = set.iterator();
			while (iterator.hasNext()) {
				String createTime = iterator.next().toString();

				if (newOrgOrganise.getCreateTime().equals(createTime)) {

					Iterator organisIterator = organisSet.iterator();
					String[] str = new String[3];
					while (organisIterator.hasNext()){
						String organiseName = organisIterator.next().toString();
						if(newOrgOrganise.getOrganiseName().equals(organiseName)){
							str[0] = newOrgOrganise.getNum()+"";
							str[1] = newOrgOrganise.getLrr();
							str[2] = newOrgOrganise.getYoy();
							timeMap.put(organiseName+","+createTime,str);
                            System.out.println("organiseName="+organiseName);
							System.out.println("str="+str);
						}
					}

				}

			}
		}

		Map map = new HashMap();
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			String createTime = iterator.next().toString();
			Map dataMap = new HashMap();
			for (Object key : timeMap.keySet()) {
				if(key.toString().split(",")[1].equals(createTime)){
					dataMap.put(key.toString().split(",")[0],timeMap.get(key));
				}
			}
			map.put(createTime,dataMap);
		}

		return map;
	}

	@Override
	public List<OrgCompetitive> getOrgCompetitiveByOrgCode(String orgCode) {
		return orgDaoMapper.getOrgCompetitiveByOrgCode(orgCode);
	}

	@Override
	public List<OrgEachCompetitive> getOrgEachCompetitiveByOrgCode(String orgCode) {

		if( orgDaoMapper.getCompetitiveIdByOrgCode(orgCode)!=null) {
			return orgDaoMapper.getOrgEachCompetitiveByCompetitiveId(orgDaoMapper.getCompetitiveIdByOrgCode(orgCode));
		}else {
			return null;
		}
	}

	@Override
	public String getAnalysisReportByOrgCode(String orgCode) {
		return orgDaoMapper.getAnalysisReportByOrgCode(orgCode);
	}

	@Override
	public String getAnalysisReportByIndustryCode(String industryCode){
		return orgDaoMapper.getAnalysisReportByIndustryCode(industryCode);
	}

	@Override
	public List<Zone> getAllZone() {
		return orgDaoMapper.getAllZone();
	}

	@Override
	public List<Industry> getAllIndustry() {
		return orgDaoMapper.getAllIndustry();
	}

	@Override
	public List<Organization> getOrgByZoneAndIndustry(String zoneCode, String industryCode) {
		List<Organization> list = orgDaoMapper.getOrgByZoneAndIndustry(zoneCode,industryCode);
		for(Organization organization:list){
			if(organization.getMedia()!=null && !"".equals(organization.getMedia())) {
				organization.setMedia(env.getProperty("profile.media_url") + organization.getMedia().substring(1));
			}
		}
		return list;
	}

	@Override
	public List<Organization> getOrgListByOrganiseZoneCode(String county, String code) {
		List<Organization> list =  orgDaoMapper.getOrgListByOrganiseZoneCode(county, code);
		for(Organization organization:list){
			if(organization.getMedia()!=null && !"".equals(organization.getMedia())) {
				organization.setMedia(env.getProperty("profile.media_url") + organization.getMedia().substring(1));
			}
		}
		return list;
	}

	@Override
	public List<OrgTest> getOrgTestListByOrganiseZoneCode(String county, String code) {
		List<OrgTest> list = orgDaoMapper.getOrgTestListByOrganiseZoneCode(county, code);
		for(OrgTest orgTest:list){
			if(orgTest.getMedia()!=null && !"".equals(orgTest.getMedia())) {
				orgTest.setMedia(env.getProperty("profile.media_url") + orgTest.getMedia().substring(1));
			}
		}
		return  list;
	}

	@Override
	public List<SalesVolume> getSalesVolumeByOrgCode(String orgCode) {
		return orgDaoMapper.getSalesVolumeByOrgCode(orgCode);
	}

	@Override
	public List<SalesVolume> getSalesVolumeByIndustryCode(String county,String industryCode){
		return orgDaoMapper.getSalesVolumeByIndustryCode(county,industryCode);
	}

	@Override
	public List<SalesTurnover> getSalesTurnoverByOrgCode(String orgCode) {
		return orgDaoMapper.getSalesTurnoverByOrgCode(orgCode);
	}

	@Override
	public List<SalesTurnover> getSalesTurnoverByIndustryCode(String county,String industryCode){
		return orgDaoMapper.getSalesTurnoverByIndustryCode(county,industryCode);
	}

	@Override
	public List<QualityCredit> getQualityCreditByOrgCode(String orgCode) {
		return orgDaoMapper.getQualityCreditByOrgCode(orgCode);
	}

	@Override
	public List<QualityCredit> getQualityCreditByIndustryCode(String county,String industryCode){
		return orgDaoMapper.getQualityCreditByIndustryCode(county,industryCode);
	}

	@Override
	public Map getProductionLicenseByOrgCode(String orgCode) {
		List<ProductionLicense> list =  orgDaoMapper.getProductionLicenseByOrgCode(orgCode);
		Set<String> set = new HashSet<String>();
		for(ProductionLicense productionLicense:list){
			set.add(productionLicense.getYear());
		}
		Iterator iterator = set.iterator();
		Map map = new HashMap();
		while (iterator.hasNext()) {
			String year = iterator.next().toString();
			String[] str = new String[3];
			for(ProductionLicense productionLicense:list) {
				if (productionLicense.getYear().equals(year)) {
					if(productionLicense.getType()==0){
						str[0] = productionLicense.getValue()+"";
					}else if(productionLicense.getType()==1){
						str[1] = productionLicense.getValue()+"";
					}else  if(productionLicense.getType()==2){
						str[2] = productionLicense.getValue()+"";
					}
				}
			}
			map.put(year,str);
		}

        return map;
	}

	public Map getProductionLicenseByIndustryCode(String county,String industryCode){
		List<ProductionLicense> list =  orgDaoMapper.getProductionLicenseByIndustryCode(county,industryCode);
		Set<String> set = new HashSet<String>();
		for(ProductionLicense productionLicense:list){
			set.add(productionLicense.getYear());
		}
		Iterator iterator = set.iterator();
		Map map = new HashMap();
		while (iterator.hasNext()) {
			String year = iterator.next().toString();
			String[] str = new String[3];
			for(ProductionLicense productionLicense:list) {
				if (productionLicense.getYear().equals(year)) {
					if(productionLicense.getType()==0){
						str[0] = productionLicense.getValue()+"";
					}else if(productionLicense.getType()==1){
						str[1] = productionLicense.getValue()+"";
					}else  if(productionLicense.getType()==2){
						str[2] = productionLicense.getValue()+"";
					}
				}
			}
			map.put(year,str);
		}

		return map;
	}

	@Override
	public List<Proportion> getProportionByOrgCode(String orgCode) {
		return orgDaoMapper.getProportionByOrgCode(orgCode);
	}

	@Override
	public List<Proportion> getProportionByIndustryCode(String county,String industryCode){
		return orgDaoMapper.getProportionByIndustryCode(county,industryCode);
	}

	@Override
	public Map getConsumeByOrgCode(String orgCode) {
		List<Consume> list = orgDaoMapper.getConsumeByOrgCode(orgCode);
		Set<String> set = new HashSet<String>();
		for(Consume consume:list){
			set.add(consume.getYear());
		}
		Iterator iterator = set.iterator();
		Map map = new HashMap();
		while (iterator.hasNext()) {
			String year = iterator.next().toString();
			String[] str = new String[4];
			for(Consume consume:list) {
				if (consume.getYear().equals(year)) {
					if(consume.getType()==0){
						str[0] = consume.getValue()+"";
					}else if(consume.getType()==1){
						str[1] = consume.getValue()+"";
					}else  if(consume.getType()==2){
						str[2] = consume.getValue()+"";
					}else if(consume.getType()==3){
						str[3] = consume.getValue()+"";
					}
				}
			}
			map.put(year,str);
		}

		return map;
	}

	@Override
	public Map getConsumeByIndustryCode(String county,String industryCode) {
		List<Consume> list = orgDaoMapper.getConsumeByIndustryCode(county,industryCode);
		Set<String> set = new HashSet<String>();
		for(Consume consume:list){
			set.add(consume.getYear());
		}
		Iterator iterator = set.iterator();
		Map map = new HashMap();
		while (iterator.hasNext()) {
			String year = iterator.next().toString();
			String[] str = new String[4];
			for(Consume consume:list) {
				if (consume.getYear().equals(year)) {
					if(consume.getType()==0){
						str[0] = consume.getValue()+"";
					}else if(consume.getType()==1){
						str[1] = consume.getValue()+"";
					}else  if(consume.getType()==2){
						str[2] = consume.getValue()+"";
					}else if(consume.getType()==3){
						str[3] = consume.getValue()+"";
					}
				}
			}
			map.put(year,str);
		}

		return map;
	}

	@Override
	public List<Profile> getProfile() {
		List<Profile> list =  orgDaoMapper.getProfile();
		for(Profile profile:list){
			if(profile.getMedia()!=null && !"".equals(profile.getMedia())) {
				profile.setMedia(env.getProperty("profile.media_url") + profile.getMedia().substring(1));
			}
		}
		return list;
	}

}
